package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageAllocatingListActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageAllocatingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingListBean;

import java.util.List;

/**
 * 作者：wsy on 2017/12/1 17:00
 * <p>
 * 邮箱：445979770@qq.com
 */

public class AllocatingInStorageListAdapter extends BaseAdapter {


    private Context mContext;
    private List<InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean> mAllocatingInStorageList;
    private LayoutInflater inflater;
    private InStorageAllocatingListActivity.InStorageAllocatingListener mInStorageAllocatingListener;
    private String mInStorageNumber;

    public AllocatingInStorageListAdapter(Context context, List<InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean> allocatingInStorageList, String inStorageNumber, InStorageAllocatingListActivity.InStorageAllocatingListener inStorageAllocatingListener) {
        mContext = context;
        mAllocatingInStorageList = allocatingInStorageList;
        mInStorageAllocatingListener = inStorageAllocatingListener;
        inflater = LayoutInflater.from(context);
        mInStorageNumber = inStorageNumber;
    }

    @Override
    public int getCount() {
        return mAllocatingInStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mAllocatingInStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_allocating_list_first_item, null);
            EditText inStorageNumber = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            inStorageNumber.setText(mInStorageNumber == null ? "" : mInStorageNumber);
            inStorageNumber.setHint(convertView.getResources().getString(R.string.activity_in_storage_allocating_list_please_enter_the_dial_number_or_the_distribution_number));
//            inStorageNumber.addTextChangedListener(new QRCodeEditTextWatcher(inStorageNumber) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, inStorageNumber);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mInStorageAllocatingListener.onNoDoubleClickListener(inStorageNumber.getText().toString());
                }
            });
        } else {
            InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean allocatingInstorageItem = mAllocatingInStorageList.get(position - 1);
            AllocatingInStorageListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_allocating_list, null);
                viewHolder = new AllocatingInStorageListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (AllocatingInStorageListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, allocatingInstorageItem);
        }
        return convertView;
    }

    private void initView(AllocatingInStorageListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.allocatingNumber = (TextView) view.findViewById(R.id.allocatingNumber);
        viewHolder.deliveryNumber = (TextView) view.findViewById(R.id.deliveryNumber);
        viewHolder.sendFatory = (TextView) view.findViewById(R.id.sendFatory);
        viewHolder.sendLocation = (TextView) view.findViewById(R.id.sendLocation);
        viewHolder.inStorageNumber = (TextView) view.findViewById(R.id.inStorageNumber);
    }

    private void initData(Context context, AllocatingInStorageListAdapter.ViewHolder viewHolder, View view, InStorageAllocatingListBean.ReceiveInAllocatingStorageListBean allocatingInStorageItem) {
        viewHolder.allocatingNumber.setText(allocatingInStorageItem.getAllocate_code());
        viewHolder.deliveryNumber.setText(allocatingInStorageItem.getAllocate_delivery_code());
        viewHolder.sendFatory.setText(allocatingInStorageItem.getFty_output_name());
        viewHolder.sendLocation.setText(allocatingInStorageItem.getLocation_output_name());
//        viewHolder.inStorageNumber.setText(allocatingInStorageItem.getInStorageNumber());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageAllocatingOverviewActivity.class);
                intent.putExtra(InStorageExtra.TRANSFER_NUMBER, allocatingInStorageItem.getAllocate_id());
                intent.putExtra(InStorageExtra.DELIVERY_NUMBER, allocatingInStorageItem.getAllocate_delivery_id());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView allocatingNumber, deliveryNumber, sendFatory,sendLocation, inStorageNumber;
    }
}
